home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_05 / colton / demo_cli.c < prev    next >
C/C++ Source or Header  |  1995-02-21  |  8KB  |  490 lines

  1. /*
  2.  * Listing 3 - demo_client.c
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <rpc/rpc.h>
  7. #include <malloc.h>
  8. #include <string.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #include <ctype.h>
  12. #include <sys/time.h>
  13. #include "ips.h"
  14.  
  15. /* Prototypes */
  16. char                    *get_file();
  17. void                     image_release();
  18. void                    upcase();
  19. void                    help_text();
  20.  
  21. /* Globals */
  22. static unsigned long    image_id=-1;
  23. static struct timeval    Timeout = { 10000, 0 };
  24.  
  25.  
  26. void
  27. main(argc, argv)
  28. int argc;
  29. char **argv;
  30. {
  31.     int done = 0;
  32.     CLIENT    *cl;
  33.     char cmd[256];
  34.     Packet    *request, *reply;
  35.     int id;
  36.     char command[256];
  37.  
  38.     request = (Packet *) malloc(sizeof(Packet));
  39.  
  40.     if(argc < 2) {
  41.         printf("Usage: %s server\n", argv[0]);
  42.         exit(0);
  43.     }
  44.  
  45.     if(!(cl = clnt_create(argv[1], IPSPROG, IPSVERS,
  46.             "tcp"))) {
  47.         clnt_pcreateerror(argv[1]);
  48.         exit(1);
  49.     }
  50.  
  51.     clnt_control(cl, CLSET_TIMEOUT, (char *)&Timeout);
  52.  
  53.     request->op = IPS_INIT;
  54.     reply = ips_1(request, cl);
  55.  
  56.     if(reply)
  57.         printf("\n%s\n\n",
  58.             reply->Packet_u.hello_string);
  59.  
  60.     while(!done) {
  61.         printf("(? for help)>>");
  62.         fflush(stdout);
  63.         gets(cmd);
  64.  
  65.         if(strcmp(cmd, "help") == 0 ||
  66.                 strcmp(cmd, "?") == 0)
  67.             help_text();
  68.  
  69.         else if(strcmp(cmd, "send") == 0)
  70.             decode(cl);
  71.  
  72.         else if(strcmp(cmd, "get") == 0)
  73.             encode(cl);
  74.  
  75.         else if(strcmp(cmd, "process") == 0)
  76.             process(cl);
  77.  
  78.         else if(strcmp(cmd, "operators") == 0)
  79.             get_operators(cl);
  80.  
  81.         else if(strcmp(cmd, "loaders") == 0)
  82.             get_loaders(cl);
  83.  
  84.         else if(strcmp(cmd, "savers") == 0)
  85.             get_savers(cl);
  86.  
  87.         else if(strcmp(cmd, "quit") == 0) {
  88.             image_release(cl);
  89.             done = 1;
  90.         }
  91.         printf("\n");
  92.     }
  93. }
  94.  
  95.  
  96.  
  97. int
  98. encode(cl)
  99. CLIENT *cl;
  100. {
  101.     char buf[256];
  102.     char filename[256];
  103.     char format[40];
  104.     Packet    *reply, *request;
  105.  
  106.  
  107.     request = (Packet *) malloc(sizeof(Packet));
  108.  
  109.     printf("\nIPS Send Image:\n\n");
  110.  
  111.     printf("Enter image filename (abort to quit): ");
  112.     fflush(stdout);
  113.     gets(filename);
  114.     if(strcmp(filename, "abort") == 0)
  115.         return 1;
  116.  
  117.     printf("Enter format (abort to quit): ");
  118.     fflush(stdout);
  119.     gets(format);
  120.     if(strcmp(format, "abort") == 0)
  121.         return 1;
  122.  
  123.     upcase(format);
  124.  
  125.     request->op = IPS_REQUEST_IMAGE;
  126.     request->Packet_u.send_ops.id             = image_id;
  127.     request->Packet_u.send_ops.format         = format;
  128.     request->Packet_u.send_ops.argc         = 0;
  129.     request->Packet_u.send_ops.argv.name     = "test";
  130.     request->Packet_u.send_ops.argv.next     = NULL;
  131.  
  132.     reply = ips_1(request, cl);
  133.  
  134.     if(!reply) {
  135.         puts("Reply error or timeout.");
  136.         exit(1);
  137.     }
  138.  
  139.     if(reply->op == IPS_SEND_IMAGE) {
  140.         FILE *fp;
  141.  
  142.         fp = fopen(filename, "w");
  143.         fwrite(reply->Packet_u.img.data.data_val, 1,
  144.             reply->Packet_u.img.data.data_len, fp);
  145.         fclose(fp);
  146.  
  147.         printf("\"%s\" Received.\n", filename);
  148.  
  149.     } else if(reply->op == IPS_IMAGE_NOT_AVAIL)
  150.         printf("IPS_IMAGE_NOT_AVAIL recevied.\n");
  151.  
  152.     else if(reply->op == IPS_UNSUPPORTED_FORMAT)
  153.         printf("IPS_UNSUPPORTED_FORMAT received\n");
  154.  
  155.     else {
  156.         printf("Error in encoding.\n");
  157.         return 1;
  158.     }
  159.  
  160.     free(request);
  161.     if(reply) xdr_free(xdr_Packet, reply);
  162.  
  163.     return 0;
  164. }
  165.  
  166.  
  167. int
  168. decode(cl)
  169. CLIENT *cl;
  170. {
  171.     FILE *fp;
  172.     long size;
  173.     char buf[256];
  174.     char buf2[256];
  175.     char *filename;
  176.     unsigned char *raw_img;
  177.     Packet    *reply, *request;
  178.     
  179.  
  180.     request = (Packet *) malloc(sizeof(Packet));
  181.  
  182.     if((filename = get_file()) == NULL) {
  183.         printf("Error or aborted.\n");
  184.         return 1;
  185.     }
  186.  
  187.     printf("Please enter image format: ");
  188.     fflush(stdout);
  189.     scanf("%s", buf2);
  190.  
  191.     upcase(buf2);
  192.  
  193.     image_release(cl);
  194.  
  195.     sprintf(buf, "%s", filename);
  196.  
  197.     fp = fopen(buf, "r");
  198.     fseek(fp, 0, 2);
  199.     size = ftell(fp);
  200.  
  201.     fseek(fp, 0, 0);
  202.  
  203.     raw_img = (unsigned char *) malloc(size);
  204.     fread(raw_img, 1, size, fp);
  205.     fclose(fp);
  206.     
  207.     request->op = IPS_DECODE;
  208.     request->Packet_u.raw_img.filename = filename;
  209.     request->Packet_u.raw_img.format = strdup(buf2);
  210.     request->Packet_u.raw_img.data.data_len = size;
  211.     request->Packet_u.raw_img.data.data_val =
  212.         (char *) raw_img;
  213.  
  214.     reply = ips_1(request, cl);
  215.  
  216.     if(!reply) {     
  217.         puts("Timeout.");
  218.         exit(1);
  219.     }
  220.  
  221.     if(reply->op == IPS_OK_ID) {
  222.         image_id = reply->Packet_u.id;
  223.     } else {
  224.         printf("Error in decoding.\n\n");
  225.         return 1;
  226.     }
  227.  
  228.     free(raw_img);
  229.     free(request);
  230.     xdr_free(xdr_Packet, reply);
  231.  
  232.     return 0;
  233. }
  234.  
  235.  
  236. int
  237. process(cl)
  238. CLIENT *cl;
  239. {
  240.     int            i,
  241.                 retval;
  242.     list        *nlp, *cp;
  243.     char        buf[256];
  244.     Packet        *reply = NULL,
  245.                 *request;
  246.     int            nitems;
  247.     char        *items[10];
  248.     
  249.     if(image_id == -1) {
  250.         printf("No image sent to server.\n");
  251.         return;
  252.     }
  253.  
  254.     /*
  255.     For this article, nitems will be 0, but
  256.     this allows you to send arguments to the
  257.     operator program.  For example, if you
  258.     were executing a SCALE program, you would
  259.     have to send the new width and height.
  260.     */
  261.  
  262.     nitems = 0;
  263.  
  264.     request = (Packet *) malloc(sizeof(Packet));
  265.  
  266.     printf("Please enter processing command: ");
  267.     fflush(stdout);
  268.     scanf("%s", buf);
  269.  
  270.     upcase(buf);
  271.  
  272.     request->op                         = IPS_PROCESS;
  273.     request->Packet_u.proc.id             = image_id;
  274.     request->Packet_u.proc.command         = strdup(buf);
  275.     request->Packet_u.proc.argc         = nitems;
  276.  
  277.     if(nitems == 0) {
  278.         request->Packet_u.proc.argv.name    = "\0";
  279.         request->Packet_u.proc.argv.next    = 0;
  280.  
  281.     } else {
  282.         nlp     = &request->Packet_u.proc.argv;
  283.         for(i=0;i<nitems;i++) {
  284.             nlp->name = strdup(items[i]);
  285.             cp = nlp;
  286.             nlp->next = (list *) malloc(sizeof(list));
  287.             nlp = nlp->next;
  288.         }
  289.         cp->next = nlp = NULL;
  290.     }
  291.  
  292.     reply = ips_1(request, cl);
  293.  
  294.     free(request);
  295.  
  296.     if(!reply) {
  297.         puts("Encode timeout.");
  298.         return -1;
  299.     }
  300.  
  301.     if(reply->op == IPS_PROCESS_OK)
  302.         retval = 0;
  303.     else {
  304.         puts("There was an error processing.\n");
  305.         retval = -1;
  306.     }
  307.  
  308.     xdr_free(xdr_Packet, reply);
  309.  
  310.     return retval;
  311. }
  312.  
  313.  
  314. int
  315. get_loaders(cl)
  316. CLIENT *cl;
  317. {
  318.     list    *nlp;
  319.     Packet    *reply, *request;
  320.  
  321.  
  322.     request = (Packet *) malloc(sizeof(Packet));
  323.     
  324.     request->op = IPS_REQUEST_LOADERS;
  325.     reply = ips_1(request, cl);
  326.  
  327.     switch(reply->op) {
  328.         case IPS_SEND_LOADERS:
  329.             printf("\nIPS Load Formats:\n\n");
  330.             for(nlp =
  331.                 &reply->Packet_u.operators;nlp !=
  332.                     NULL; nlp = nlp->next)
  333.                 printf(" %s\n", nlp->name);
  334.             printf("\n");
  335.             break;
  336.  
  337.         case ERROR:
  338.             printf("get_loaders failed.\n");
  339.             return 1;
  340.     }
  341.  
  342.     free(request);
  343.     xdr_free(xdr_Packet, reply);
  344.  
  345.     return 0;
  346. }
  347.  
  348. int
  349. get_savers(cl)
  350. CLIENT *cl;
  351. {
  352.     list    *nlp;
  353.     Packet    *reply, *request;
  354.  
  355.  
  356.     request = (Packet *) malloc(sizeof(Packet));
  357.     
  358.     request->op = IPS_REQUEST_SAVERS;
  359.     reply = ips_1(request, cl);
  360.  
  361.     switch(reply->op) {
  362.         case IPS_SEND_SAVERS:
  363.             printf("\nIPS Save Formats:\n\n");
  364.             for(nlp =
  365.                 &reply->Packet_u.operators;nlp !=
  366.                     NULL; nlp = nlp->next)
  367.                 printf(" %s\n", nlp->name);
  368.             printf("\n");
  369.             break;
  370.  
  371.         case ERROR:
  372.             printf("get_savers failed.\n");
  373.             return 1;
  374.     }
  375.  
  376.     free(request);
  377.     xdr_free(xdr_Packet, reply);
  378.  
  379.     return 0;
  380. }
  381.  
  382.  
  383. int
  384. get_operators(cl)
  385. CLIENT *cl;
  386. {
  387.     list    *nlp;
  388.     Packet    *reply, *request;
  389.  
  390.  
  391.     request = (Packet *) malloc(sizeof(Packet));
  392.     
  393.     request->op = IPS_REQUEST_OPS;
  394.     reply = ips_1(request, cl);
  395.  
  396.     switch(reply->op) {
  397.         case IPS_SEND_OPS:
  398.             printf("\nIPS Processing Tools:\n\n");
  399.             for(nlp =
  400.                 &reply->Packet_u.operators;nlp !=
  401.                     NULL; nlp = nlp->next)
  402.                 printf(" %s\n", nlp->name);
  403.             printf("\n");
  404.             break;
  405.         case ERROR:
  406.             printf("get_operators failed.\n");
  407.             return 1;
  408.     }
  409.  
  410.     free(request);
  411.     xdr_free(xdr_Packet, reply);
  412.  
  413.     return 0;
  414. }
  415.  
  416.  
  417. char *
  418. get_file()
  419. {
  420.     int status;
  421.     char filename[1024], command[1024];
  422.  
  423.     printf("\nIPS Receive Image:\n\n");
  424.     printf("Type 'abort' to cancel.  Filename: ");
  425.     fflush(stdout);
  426.     gets(filename);
  427.  
  428.     if(strcmp(filename, "abort") == 0)
  429.         return NULL;
  430.  
  431.     if(access(filename, R_OK) == 0)
  432.         return strdup(filename);
  433.  
  434.     return NULL;
  435. }
  436.  
  437.  
  438. void
  439. image_release(cl)
  440. CLIENT *cl;
  441. {
  442.     Packet  *request, *reply;
  443.  
  444.     request = (Packet *) malloc(sizeof(Packet));
  445.  
  446.     if(image_id != -1) {
  447.         request->op             = IPS_IMAGE_RELEASE;
  448.         request->Packet_u.id    = image_id;
  449.         reply                   = ips_1(request, cl);
  450.  
  451.         if(reply->op == IPS_RELEASE_OK)
  452.             printf("Release of %d ok.\n", image_id);
  453.     }
  454.  
  455.     free(request);
  456. }
  457.  
  458.  
  459.  
  460. void
  461. upcase(text)
  462. char *text;
  463. {
  464.     char *p = text;
  465.  
  466.     while(*p != NULL) {
  467.         *p = toupper(*p);
  468.         p++;
  469.     }
  470. }
  471.  
  472.  
  473. void
  474. help_text()
  475. {
  476.     printf("\n");
  477.     printf("  Cmd           Description\n");
  478.     printf("--------   -----------------------\n");
  479.     printf(" send      send an image to the IPS\n");
  480.     printf(" get       receive an image from IPS\n");
  481.     printf("           in a certain format\n");
  482.     printf(" process   proce